//
//  EmojiPanelVC.m
//  oschina_song
//
//  Created by song on 16/2/15.
//  Copyright © 2016年 song. All rights reserved.
//

#import "EmojiPanelVC.h"
#import "UIColor+utils.h"


@interface EmojiPanelVC () <UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout>

@property(nonatomic , strong) UICollectionView *emojiCollectionView;


@end


@implementation EmojiPanelVC


-(id)initWithPageIndex:(int)pageIndex
{
    self = [super  init];
    if(self)
    {
        _pageIndex = pageIndex;
    }
    
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor themeColor];
    UICollectionViewFlowLayout *flowlayout =[UICollectionViewFlowLayout new];
    CGFloat screenWidth = [ UIScreen mainScreen].bounds.size.width;
    flowlayout.minimumInteritemSpacing = (screenWidth - 40 - 30 * 7) / 7;
    flowlayout.minimumLineSpacing = 10;
    flowlayout.sectionInset =UIEdgeInsetsMake(15, 0, 5, 0);
    
    _emojiCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowlayout];
    [_emojiCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"EmojiCell"];
    _emojiCollectionView.scrollEnabled = NO;
    _emojiCollectionView.delegate = self;
    _emojiCollectionView.dataSource = self;
    _emojiCollectionView.bounces = NO;
    [self.view addSubview:_emojiCollectionView];

    _emojiCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = NSDictionaryOfVariableBindings(_emojiCollectionView);
    
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[_emojiCollectionView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-20-[_emojiCollectionView]-20-|" options:0 metrics:nil views:views]];
}


-(NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSInteger left = 123 - _pageIndex * 20;
    left = left >= 20 ? 3 : (left + 7) / 7;
    return left;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger left = 123 - _pageIndex * 20 - section * 7;
    left = left >= 7 ? 7 : left + 1;
    return left;
}

-(CGSize) collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return CGSizeMake(30, 30);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EmojiCell" forIndexPath:indexPath];
    if (section == [self numberOfSectionsInCollectionView:collectionView] - 1&&
        row == [self collectionView:collectionView numberOfItemsInSection:section] - 1) {
        [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"delete"]]];
    }else{
        NSInteger emojiNum = _pageIndex *20 +section *7 + row+1;
        NSString *emojiImageName;
        if(emojiNum < 106)
        {
            emojiImageName =[NSString stringWithFormat:@"%03ld",(long)emojiNum];
            NSLog(@"%@",emojiImageName);
            [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:emojiImageName]]];
        }
        
        
    }
    
   
    return cell;
}

@end
